package cn.wellsun.util;
import java.util.Random;
/**
 * @author zchome
 */
public class RandomUtil {
	public static String random(int n) {
		if (n < 0 || n > 4) {
			throw new IllegalArgumentException("cannot random " + n + " bit number");
		}
		Random ran = new Random();
		if (n == 1) {
			return String.valueOf(ran.nextInt(4));
		}
		int bitField = 0;
		char[] chs = new char[n];
		for (int i = 0; i < n; i++) {
			while(true) {
				int k = ran.nextInt(4);
				if( (bitField & (1 << k)) == 0) {
					bitField |= 1 << k;
					chs[i] = (char)(k + '0');
					break;
				}
			}
		}
		return new String(chs);
	}
	public static void main(String[] args) {
		String s = random(4);
		System.out.println(s);
		System.out.println(s.substring(3));
		System.out.println(s.substring(2,3));
		System.out.println(s.substring(1,2));
		System.out.println(s.substring(0,1));
	}
}
